package com.groupseven.backend.controller;


import com.groupseven.backend.domain.Paper.Paper;
import com.groupseven.backend.domain.ResultType;
import com.groupseven.backend.domain.User.User;
import com.groupseven.backend.services.AuthoritiesService;
import com.groupseven.backend.services.PaperService;
import com.groupseven.backend.services.UserService;
import com.groupseven.backend.test.DataEncode;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/v1/Papers")
public class PaperController {
    @Autowired
    private UserService userService;
    @Autowired
    private PaperService paperService;
    @Autowired
    private AuthoritiesService authoritiesService;
    @Autowired
    private DataEncode dataEncode;

    /**
     * 查看我的论文------教师功能
     * @Author 王志华
     * @param username
     * @return
     */
    @GetMapping("/ways1/{username}")
    public ResultType getMyPaperInfo(@PathVariable("username") String username){
        if(userService.selectByUsername(dataEncode.encodeBae64(username))==null){
            return new ResultType(0,"查询失败，该用户不存在",200);
        }
        else {
            List<Paper> tmp= paperService.selectByPaperusername(username);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPapers(tmp);
            return resultType;
        }
    }

    /**
     * 查看全部论文------管理员功能
     * @Author 王志华
     * @return
     */
    @GetMapping("/")
    public ResultType getAllPapersInfo(){
        List<Paper> tmp = paperService.getAllPapersInfo();
        return new ResultType(1,"查询成功",200,null,null,null,null,tmp);
    }

    @GetMapping("/ways2/{PaperName}")
    public ResultType getByPaperName(@PathVariable("PaperName") String PaperName){
        if (paperService.selectByPaperName(PaperName)==null||paperService.selectByPaperName(PaperName).isEmpty()){
            return new ResultType(0,"查询失败,论文名不存在",200);
        }
        else {
            List<Paper> tmp =paperService.selectByPaperName(PaperName);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPapers(tmp);
            return resultType;
        }
    }

    @GetMapping("/ways3/{PaperNumber}")
    public ResultType getByPaperNumber(@PathVariable("PaperNumber") String PaperNumber){
        if(paperService.selectByPaperNumber(PaperNumber)==null){
            return new ResultType(0,"查询失败,论文编号不存在",200);
        }
        else {
            Paper paper=paperService.selectByPaperNumber(PaperNumber);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPaper(paper);
            return resultType;
        }
    }
    /**
     * 增添论文
     * @Author 王志华
     * @param paper
     * @return
     */
    @PostMapping("/")
    public ResultType AddPaperInfo(@RequestBody @Valid Paper paper){
//        List<String> operationAuthorities = authoritiesService.getMyOperationAuthorities(user);
//        for(String i : operationAuthorities){
//            if(i.equals("add_papers")){
//                List<Paper> tmp = new ArrayList<>();
//                tmp.add(paper);
//                for (Paper u : tmp){
//                    System.out.println(u.getPaperName());
//                }
//                if(paperService.insertPaperInfo(paper)>0) {
//                    return new ResultType(1, "插入成功！", 200, null, null, null, null,tmp);
//                }
//                return new ResultType(0, "该论文已存在！插入失败！", 200, null, null, null, null,null);
//            }
//        }
//        return new ResultType(0,"无权限",200,null,null,null,null,null);
        Subject subject= SecurityUtils.getSubject();
        User user=(User)subject.getPrincipal();
        paper.setUsername(user.Username);
        paper.setIsfinish(2);
        paper.setApprovalStatus(2);
        /////////////////////////////////////////////
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss:SSS");
        paper.setPaperNumber(formatter.format(new Date()));
        /////////////////////////////////////////////
        Date date=new Date();
        paper.setStartTime(date);
        List<Paper> tmp = new ArrayList<>();
        tmp.add(paper);
        if(paperService.insertPaperInfo(paper)>0) {
            return new ResultType(1, "插入成功！", 200, null, null, null, null,tmp);
        }

        return new ResultType(0, "该论文已存在！插入失败！", 200, null, null, null, null,null);

    }

    /**
     * 修改论文
     * @Author 王志华
     * @param paper
     * @return
     */
    @PutMapping("/")
    public ResultType updatePaperInfo(@RequestBody @Valid Paper paper){
        if (paperService.updatePaper(paper)==0){
            return new ResultType(0,"改动失败,论文编号不存在",200);
        }
        else if(paperService.updatePaper(paper)==-1) {
            return new ResultType(0, "改动失败,用户名不存在", 200);
        } else {
            return new ResultType(1,"改动成功",200);
        }
    }

    /**
     * 删除论文
     * @Author 王志华
     * @param PaperNumber
     * @return
     */
    @DeleteMapping("/{PaperNumber}")
    public ResultType deletePaperInfo(@PathVariable("PaperNumber") String PaperNumber){
        if(paperService.deleteByPaperNumber(PaperNumber)==0){
            return new ResultType(0,"删除失败",200);
        }
        else {
            return new ResultType(1,"删除成功",200);
        }
    }

    @DeleteMapping("/")
    public ResultType deleteallPapers(){
        paperService.deleteallPapers();
        return new ResultType(1,"删除成功",200);
    }

    //////////////////////////////////////////////////////////////////////////////
    @PostMapping("/s/")
    public ResultType mutildelPapers(@RequestBody List<Paper> papers){
        List<String> PaperNumbers=new ArrayList<>();
        for(Paper paper :papers){
            PaperNumbers.add(paper.PaperNumber);
        }

        if(paperService.mutildelPapers(PaperNumbers)==0){
            return new ResultType(0,"删除失败",200);
        }
        else {
            return new ResultType(1,"删除成功",200);
        }
    }
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    @GetMapping("/likefind/ways2/{PaperName}")
    public ResultType getlikeByPaperName(@PathVariable("PaperName") String PaperName){
        if (paperService.getlikeByPaperName(PaperName).isEmpty()){
            return new ResultType(0,"查询失败,论文名不存在",200);
        }
        else {
            List<Paper> tmp =paperService.getlikeByPaperName(PaperName);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPapers(tmp);
            return resultType;
        }
    }
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    @GetMapping("/likefind/ways1/{username}")
    public ResultType getlikeMyPaperInfo(@PathVariable("username") String username){
        if(paperService.getlikeMyPaperInfo(username).isEmpty()){
            return new ResultType(0,"查询失败，该用户不存在",200);
        }
        else {
            List<Paper> tmp= paperService.getlikeMyPaperInfo(username);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPapers(tmp);
            return resultType;
        }
    }
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    @GetMapping("/likefind/ways3/{PaperNumber}")
    public ResultType getlikeByPaperNumber(@PathVariable("PaperNumber") String PaperNumber){
        if(paperService.getlikeByPaperNumber(PaperNumber).isEmpty()){
            return new ResultType(0,"查询失败,论文编号不存在",200);
        }
        else {
            List<Paper> papers=paperService.getlikeByPaperNumber(PaperNumber);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPapers(papers);
            return resultType;
        }
    }
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    @GetMapping("/likefind/ways4/{AllAuthors}")
    public ResultType getlikeByAllAuthors(@PathVariable("AllAuthors") String AllAuthors){
        if(paperService.getlikeByAllAuthors(AllAuthors).isEmpty()){
            return new ResultType(0,"查询失败,论文编号不存在",200);
        }
        else {
            List<Paper> papers=paperService.getlikeByAllAuthors(AllAuthors);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPapers(papers);
            return resultType;
        }
    }
    //////////////////////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////////////////////
    @GetMapping("/search/{query}")
    public ResultType searchqueryPaper(@PathVariable String query){
        if(paperService.searchqueryPaper(query).isEmpty()){
            return new ResultType(0,"查询失败",200);
        }
        else {
            List<Paper> papers=paperService.searchqueryPaper(query);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPapers(papers);
            return resultType;
        }
    }
///////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////

    @GetMapping("/search/{username}/{query}")
    public ResultType searchuserqueryPaper(@PathVariable String query,@PathVariable String username){
        if(paperService.searchuserqueryPaper(query,username).isEmpty()){
            return new ResultType(0,"查询失败",200);
        }
        else {
            List<Paper> papers=paperService.searchuserqueryPaper(query,username);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPapers(papers);
            return resultType;
        }
    }
// ////////////////////////////////////////////////////////////////
}
